import { Component, OnInit, ViewChild, ElementRef, OnDestroy } from '@angular/core';
import { Router, NavigationEnd, ActivatedRoute } from '@angular/router';

import { IonContent, AlertController, ModalController } from '@ionic/angular';

import { ActionSheetService, Modal } from 'ng-zorro-antd-mobile';

import { OpenAppComponent } from '../coms/open-app/open-app.component';

import { SharePageComponent } from '../coms/share-page/share-page.component';
import { ComItem } from '../route/entry/com-item';
import { AppGlobalService } from '../shared/service/app-global.service';
import { EventService } from '../shared/service/event.service';
import { HttpService } from '../shared/service/http.service';
import { TabsService } from '../shared/service/tabs.service';

@Component({
  selector: 'app-laijian-login',
  templateUrl: '../route/entry/entry.page.html',
  styleUrls: ['../route/entry/entry.page.scss'],
})
export class LaiJianLoginComponent implements OnInit, OnDestroy {
  isLoading = false;
  headerComs: ComItem[];
  menusBar: any;
  coms: ComItem[];
  subComs: ComItem[];
  footerComs: ComItem[];
  sessionId: string;
  params: any = '';
  viewId: any;
  // 标签头数据
  titleBar: any;
  isHeader = true;
  isGrayBg = false; // 默认背景为灰色
  bgCssStyle = ''; // 默认背景为灰色
  isPageDisabled = false;
  backgroundImg: string; // 头部背景图片
  showWaterImg = false;
  showRefresher = false; // 下拉刷新
  showLoading = false;
  showEmptyMessage = false;

  // 位置对象
  positionInfo: any = {};
  // 小区位置信息
  areaPositionInfo: any = {};
  showTopTab = false; // 是否显示顶部tab
  showSubTab = false; // 是否显示Subtab
  subTagData: any; // 顶部tab data
  subAWidth: string;
  tagData: any;
  aWidth: string;
  viewParams: any;
  isFirst = true;
  openBackdrop: boolean;
  showRefresh = false; // 是否显示刷新
  netState: any; // 网络状态
  isScrolling = false;
  private timer: any;
  // 路由对象
  routerSub: any;

  // 分享页面数据
  shareData = {
    icon: "share.png",
    layout: "right",
    submit: {
      content: {},
      id: "string"
    }
  };
  showScrollToTopBtn = false; // 是否显示滑动到顶部按钮
  isAddCommonNestedTag = true;
  showDivTips = false;
  @ViewChild('superCotainer') scrollContent: ElementRef;
  @ViewChild(IonContent) content: IonContent;
  serviceLink: any;
  subTagDataId = 'subTagDataId';
  switchNewCssBtnId = 'switchNewCssBtnId';
  constructor(
    private router: Router,
    private route: ActivatedRoute,
    public alertController: AlertController,
    public appGlobalService: AppGlobalService,
    private modalController: ModalController,
    private eventService: EventService,
    private http: HttpService,
    private tabs: TabsService) { }

  ngOnDestroy(): void {
    // throw new Error('Method not implemented.');
    if (localStorage.getItem('backToLaiJianLogin')) {
      localStorage.removeItem('backToLaiJianLogin');
    }
  }

  ngOnInit() {
    this.subTagDataId += new Date().getTime();
    this.switchNewCssBtnId += new Date().getTime();
    this.openBackdrop = this.appGlobalService.openBackdrop;
    // 订阅路由
    this.routerSub = this.router.events.subscribe((event: NavigationEnd) => {
      if (localStorage.getItem('backToLaiJianLogin')) {
        return;
      }
      if (event instanceof NavigationEnd) {
        // 判断是否有参数
        if (this.route.queryParams) {
          // 路由取值
          this.route.queryParams.subscribe(queryParams => {
            console.log(queryParams.params);
            const item = JSON.parse(queryParams.item);
            // 存在参数
            if (item.params) {
              const param = {
                pageId: item.pageId ? item.pageId : item.target,
                params: item.params, // update by andrew.wen for  案例分享，合同id参数没传过去，导致打开分享链接是空白页
                sessionId: localStorage.getItem('sessionId')
              };
              this.getPage(param);
            }
          });
        }
      }
    });
  }

  //  获取详情页面
  getPage(params) {
    this.http.post('application/page', params, res => {
      // console.log(res);
      this.tabs.getPageData(res, pageData => {
        // console.log('new-shop=' + JSON.stringify(pageData));
        this.titleBar = pageData.titleBar;
        this.coms = pageData.coms;
        this.showRefresher = pageData.showRefresher;
        this.showLoading = pageData.showLoading;
        this.footerComs = pageData.footerComs;
      });
    });
  }

  back() {
    this.appGlobalService.goBack();
  }
  scrollEnd(e: any) {

  }

  onScroll(e: any,titleBar?) {
    this.isScrolling = true;
    }

  // 滑动到顶部
  scrollToTop(event) {
    event.stopPropagation();
    this.content.scrollToTop(1000);
  }
  gotoEnterpriseWechat(e) { }
  showTipBox(type) {
    this.appGlobalService.showTipBox(type);
  }
  changeCity(item, event?) {
    if (event !== undefined) {
      event.stopPropagation();
    }
  }
}
